var article=$('<article class="excerpt excerpt-1"><div class="header"><img class="user-img" src=""><div class="name-time-wrapper"><span class="name"></span><time class="time"></time></div></div><div class="mycontext"><p class="note"></p></div><div class="ff"><div class="digg1"><a class="huifu">回 复</a></div><div class="digg1"><a class="delete">删 除</a></div></div></article>');function toMyMessage(){
	$("#content-wrap .content").find("article").remove();
	$.post("getMyLetter","",function(data){
		//alert(data);
		data=JSON.parse(data);
		
		//排序开始
		var arr=[];
		var count=0;
		for(var key in data) {
			arr[count]=[key,data[key][data[key].length-1].sendTime];
			count++;
	    }
		for(var i=0;i<arr.length;i++){
			for(var j=1;j<arr.length;j++){
				if(arr[i][1]<arr[j][1]){
					var temp=arr[i];
					arr[i]=arr[j];
					arr[j]=temp;
				}
			}
		}
		//排序结束
		for(var i=0;i<arr.length;i++){
			var dataTemp=data[arr[i][0]];
			var master=arr[i][0].split(":")[1];
			var temp=article.clone(false);
			var flag=dataTemp.length-1;
			var time;
			time=dataTemp[0].sendTime;
			for(var k=1;k<dataTemp.length;k++){
				var obj=dataTemp[k];
				if(time<obj.sendTime){
					time=dataTemp[k].sendTime;
				}
			}
			for(var j=dataTemp.length-1;j>=0;j--){
				var obj=dataTemp[j];
				if(obj.writerUser.userId!=master){
					temp.find(".header .name-time-wrapper").append($('<input type="hidden" value="'+obj.letterId+'"><input type="hidden" name="parentId" value="'+arr[i][0].split(":")[0]+'">'));
					temp.find(".header .name-time-wrapper .name").text(obj.writerUser.userName);
					temp.find(".header .name-time-wrapper .time").html('<i class="glyphicon glyphicon-time"></i>'+time);
					temp.find(".mycontext .note").text(obj.content);
					flag=j;
					break;
				}
			}
			var listener=[];
			var listenerNum=0;
			for(var j=dataTemp.length-1;j>=0;j--){
				if(j==flag){
					continue;
				}
				var obj=dataTemp[j];
				temp.find(".ff").before($('<div class="childcontext"><input type="hidden" name="children" value="'+obj.letterId+'"><span class="note"><span class="name">'+obj.sendTime+'&nbsp;'+obj.writerUser.userName+':</span>&nbsp;&nbsp;'+obj.content+'</span></div>'));
				if(obj.writerUser.userId==master){
					listener[listenerNum]=j;
					listenerNum++;
				}
			}
			addChileDeleteListener(temp.find(".childcontext"),listener);
			addDeleteListener(temp.find(".delete"));
			addCommentListener(temp.find(".huifu"));
			$("#content-wrap .content").append(temp);
		}
	});
	return false;
}

function addChileDeleteListener(obj,listener){
	for(var i=0;i<listener.length;i++){
		obj[i].onclick=function(){
			if($(this).find(".digg").prop("class")!=undefined){
				$(this).find(".digg").remove();
			}else{
				$(this).append('<div class="digg childrendigg"><a class="delete">删 除</a></div>');
			}
		};
	}	
}

function addDeleteListener(obj){
	obj.on("click",function(){
		var articleTemp=$(this).parents("article");
		var letterId=articleTemp.find(".header .name-time-wrapper").find("[type=hidden]").val();
		var childrendigg=$(this).parent().parent()
		var temp=childrendigg.find("[type=hidden]").val();
		if(temp!=undefined){
			letterId=temp;
		}
		
		$.post("deleteLetter",{"letterId":letterId},function(data){
			if(data>0){
				if(temp!=undefined){
					childrendigg.remove();
				}else{
					articleTemp.remove();
				}
			}
		});
	})
}

function addCommentListener(obj){
	obj.on("click",function(){
		var articleTemp=$(this).parents("article");
		if(articleTemp.find(".commentdiv").prop("class")==undefined){
			$(this).text("取消回复");
			var ff=$(this).parent().parent();
			ff.after($('<div class="commentdiv"><textarea class="comment-input" placeholder="说点什么吧"></textarea><input type="button" class="digg1" style="color:rgba(200,21,34,1)" value="发 送" onclick="send(this)"></div>'));
		}else{
			$(this).text("回复");
			articleTemp.find(".commentdiv").remove();
		}
	})
}

function send(obj){
	var articleTemp=$(obj).parents("article");
	var letterId=articleTemp.find(".header .name-time-wrapper").find("[type=hidden]").val();
	var content=$(obj).prev().val();
	if(content==""){
		return;
	}
	var parentId;
	if(articleTemp.find(".childcontext").length==0){
		parentId=articleTemp.find(".header").find("[name=parentId]").val();
	}else{
		parentId=null;
	}
	
	$.post("insertLetter",{"letterId":letterId,"content":content,"parentId":parentId},function(data){
		if(data!=null){
			//alert(data);
			data=JSON.parse(data);
			$(obj).prev().val("");
			articleTemp.find(".mycontext").after($('<div class="childcontext"><input type="hidden" name="children" value="'+data.letterId+'"><span class="note"><span class="name">'+data.sendTime+'&nbsp;'+data.writerUser.userName+':</span>&nbsp;&nbsp;'+data.content+'</span></div>'));
		}
	});
}